package com.yc.cloud.openai.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.yc.cloud.openai.dto.NlpChunkResultDto;
import com.yc.cloud.openai.dto.OpenAiResultDto;
import com.yc.cloud.openai.dto.TTSResultDTO;
import com.yc.cloud.openai.enums.SupplierEnums;
import com.yc.cloud.openai.request.IatParam;
import com.yc.cloud.openai.request.NLPParam;
import com.yc.cloud.openai.request.OpenAiParam;
import com.yc.cloud.openai.request.TTSParam;
import com.yc.cloud.openai.service.IATService;
import com.yc.cloud.openai.service.NLPService;
import com.yc.cloud.openai.service.OpenAiService;
import com.yc.cloud.openai.service.tts.TTSService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.util.Map;

@Slf4j
@Service("HFKJOpenAiServiceImpl")
public class HFKJOpenAiServiceImpl implements OpenAiService {


    private final TTSService ttsService;
    private final IATService iatService;

    private final NLPService nlpService;


    public HFKJOpenAiServiceImpl(@Qualifier("HFKJIatServiceImpl") IATService iatService, @Qualifier("HFKJNLPServiceImpl") NLPService nlpService, @Qualifier("HFKJTTSServiceImpl") TTSService ttsService) {
        this.iatService = iatService;
        this.nlpService = nlpService;
        this.ttsService = ttsService;
    }

    @Override
    public String getIdentifier() {
        return SupplierEnums.HFKJ.getCode();
    }

    @Override
    public OpenAiResultDto analysis(OpenAiParam openAiParam) {
        val openAiResultDto = new OpenAiResultDto();
        val path = openAiParam.getPath();
        //将audio文件转成base64
        //val base64Data = MyFileUtils.convertToBase64(FileUtil.file(path));
        //语音文件转音频文本内容
        val iatParam = new IatParam();
        iatParam.setContent(path);
        iatParam.setFormat(FileUtil.getSuffix(path));

        val iatResult = iatService.getIatResult(iatParam);
        if (iatResult == null) {
            return null;
        }
        val iatContent = iatResult.getResult();
        openAiResultDto.setIatContent(iatContent);
        openAiResultDto.setIatCostTime(iatResult.getCostTime());
        //语言理解接口调用
        val nlpParam = new NLPParam();
        nlpParam.setContent(iatContent);
        val nlpResult = nlpService.getNLPResult(nlpParam);
        if (nlpResult == null) {
            return openAiResultDto;
        }
        val nlpContent = nlpResult.getResult();
        openAiResultDto.setNlpContent(nlpContent);
        openAiResultDto.setNlpCostTime(nlpResult.getCostTime());
        //TTS 文本转语音
        if (StrUtil.isEmpty(nlpContent)) {
            return null;
        }
//        val ttsParam = new TTSParam();
//        ttsParam.setContent(nlpContent);
//        ttsParam.setGender(openAiParam.getGender());
//        ttsParam.setFormat(openAiParam.getFormat());
//        val ttsResult = ttsService.getTTSResult(ttsParam);
//        if (ttsResult == null) {
//            return null;
//        }
//        val ttsContent = ttsResult.getResult();
//        openAiResultDto.setTtsContent(ttsContent);
        return openAiResultDto;
    }

    @Override
    public TTSResultDTO getTTSResult(OpenAiParam openAiParam) {
        val ttsParam = new TTSParam();
        ttsParam.setContent(openAiParam.getContent());
        ttsParam.setGender(openAiParam.getGender());
        ttsParam.setFormat(openAiParam.getFormat());
        val ttsResult = ttsService.getTTSResult(ttsParam);
        if (ttsResult == null) {
            return null;
        }
        return ttsResult;
    }

    @Override
    public Flux<String > analysisByStream(OpenAiParam openAiParam) {
        return null;
    }

    @Override
    public Flux<ServerSentEvent<String>> analysisByStream2(OpenAiParam param) {
        return null;
    }

    @Override
    public SseEmitter analysisByStream3(OpenAiParam param) {
        return null;
    }
}
